package com.hexinfo.dmpro.admin.controller;

import com.hexinfo.dmpro.common.annotation.Log;
import com.hexinfo.dmpro.common.base.BaseRequest;
import com.hexinfo.dmpro.common.base.BaseView;
import com.hexinfo.dmpro.common.exception.BadRequestException;
import com.hexinfo.dmpro.admin.model.Menu;
import com.hexinfo.dmpro.admin.model.Role;
import com.hexinfo.dmpro.admin.model.User;
import com.hexinfo.dmpro.admin.service.MenuService;
import com.hexinfo.dmpro.admin.service.RoleService;
import com.hexinfo.dmpro.admin.service.UserService;
import com.hexinfo.dmpro.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @ClassName MenuController
 * @Describe
 * @Author ml.wang
 * @Date 2021-06-16
 */
@Api(tags = "系统：菜单管理")
@RestController
@RequestMapping("/api/menus")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Log("查询菜单")
    @ApiOperation("查询菜单")
    @GetMapping
    @PreAuthorize("@permission.check('menu:list')")
    public ResponseEntity<Object> getMenus(BaseView baseView){
        List<Menu> menus = menuService.queryAll(BaseRequest.viewToRequest(baseView));
        return new ResponseEntity<>(menuService.buildTree(menus), HttpStatus.OK);
    }

    @ApiOperation("根据登录用户的角色得到左侧菜单栏的菜单树")
    @GetMapping(value = "/build")
    public ResponseEntity<Object> buildMenus(){
        User user = userService.findByName(SecurityUtils.getUsername());
        List<Role> roles = roleService.findByUserId(user.getId());
        List<Menu> menus = menuService.findByRoles(roles);
        menus = (List<Menu>) menuService.buildTree(menus).get("content");
        return new ResponseEntity<>(menuService.buildMenus(menus), HttpStatus.OK);
    }

    @ApiOperation("返回全部的菜单")
    @GetMapping(value = "/tree")
    @PreAuthorize("@permission.check('menu:list','roles:list')")
    public ResponseEntity<Object> getMenuTree(){
        List<Menu> menus = menuService.findByPid(0L);
        return new ResponseEntity<>(menuService.getMenuTree(menus), HttpStatus.OK);
    }

    @Log("新增菜单")
    @ApiOperation("新增菜单")
    @PostMapping
    @PreAuthorize("@permission.check('menu:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Menu menu){
        if (menu.getId() != null) {
            throw new BadRequestException("A new menu cannot already have an ID");
        }
        menuService.create(menu);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改菜单")
    @ApiOperation("修改菜单")
    @PutMapping
    @PreAuthorize("@permission.check('menu:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Menu menu){
        menuService.update(menu);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除菜单")
    @ApiOperation("删除菜单")
    @DeleteMapping
    @PreAuthorize("@permission.check('menu:del')")
    public ResponseEntity<Object> delete(@RequestBody Set<Long> ids){
        Set<Menu> menuSet = new HashSet<>();
        for (Long id : ids) {
            List<Menu> menuList = menuService.findByPid(id);
            menuSet.add(menuService.findOne(id));
            menuSet = menuService.getDeleteMenus(menuList, menuSet);
        }
        menuService.delete(menuSet);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
